package com.ruoyi.user.api;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.thread.BaseAPIController;
import com.ruoyi.user.domain.FileManagement;
import com.ruoyi.user.service.IFileManagementService;

import io.swagger.annotations.Api;

/**
 * 	用户档案相关API
 * @author DEREK
 *
 */
@RestController
@RequestMapping("/api/v1/fileManagement")
@Api(tags = "【APP】用户档案相关接口")
@CrossOrigin
public class AppFileManagementController extends BaseAPIController{

	@Autowired
	private IFileManagementService fileManagementService;
	
	/**
	 * 	档案管理
	 * @param pageNo
	 * @param request
	 * @return
	 */
	@GetMapping(value = "/getFileManagementList")
	public AjaxResult getFileManagementList(Integer pageNo,HttpServletRequest request) {
		if(null == pageNo) pageNo = 1;
		Long userId = getUserId();
		if(null == userId) {
			return AjaxResult.error(50010,"请重新登录");
		}
		Page<FileManagement> page = fileManagementService.page(new Page<FileManagement>().setCurrent(pageNo).setSize(10), new LambdaQueryWrapper<FileManagement>().eq(FileManagement::getUserId, userId));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total",page.getTotal());
		map.put("list", page.getRecords());
		return AjaxResult.success(map);
	}
	
	/**
	 * 	添加档案库
	 * @param file
	 * @param request
	 * @return
	 */
	@PostMapping(value = "/addFileManagement")
	public AjaxResult addFileManagement(@RequestBody FileManagement file,HttpServletRequest request) {
		Long userId = getUserId();
		if(null == userId) {
			return AjaxResult.error(50010,"请重新登录");
		}
		file.setUserId(userId);
		fileManagementService.save(file);
		return AjaxResult.success();
	}
	
	/**
	 * 	编辑档案库
	 * @param file
	 * @param request
	 * @return
	 */
	@PostMapping(value = "/editFileManagement")
	public AjaxResult editFileManagement(@RequestBody FileManagement file,HttpServletRequest request) {
		if(null == file || null == file.getId()) {
			return AjaxResult.error("请选择数据");
		}
		fileManagementService.updateById(file);
		return AjaxResult.success();
	}
	/**
	 * 	删除档案库
	 * @param file
	 * @param request
	 * @return
	 */
	@PostMapping(value = "/delFileManagement")
	public AjaxResult delFileManagement(@RequestBody FileManagement file,HttpServletRequest request) {
		if(null == file || null == file.getId()) {
			return AjaxResult.error("请选择数据");
		}
		fileManagementService.deleteFileManagementById(file.getId());
		return AjaxResult.success();
	}
}
